package 滑动窗口;

public class 最大连续1的个数III3 {
    //https://leetcode.cn/problems/max-consecutive-ones-iii/
    public int longestOnes1(int[] nums, int k) {
        int left = 0, right = 0, zero = 0, n = nums.length, ret = 0;
        while(right < n) {
            if(nums[right] == 0) {
                zero++;
            }
            while(zero > k) {
                if(nums[left] == 0) {
                    zero--;
                }
                left++;
            }
            ret = Math.max(right - left + 1,ret);
            right++;
        }
        return ret;
    }
    public int longestOnes(int[] nums, int k) {
        int ret = 0;
        for(int left = 0, right = 0, zero = 0; right < nums.length; right++) {
            if(nums[right] == 0) { // 进窗口
                zero++;
            }
            while(zero > k) { // 判断
                if(nums[left++] == 0) {
                    zero--;
                }
            }
            ret = Math.max(ret,right - left + 1);
        }
        return ret;
    }
}
